package cn.restcloud.springboot.init;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;


public class SpringbootUtil  {

    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringbootUtil.applicationContext == null) {
        	SpringbootUtil.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过beanId获取 Bean.
    public static Object getBean(String beanId){
        return getApplicationContext().getBean(beanId);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过beanId,以及Clazz返回指定的Bean
    public static <T> T getBean(String beanId,Class<T> clazz){
        return getApplicationContext().getBean(beanId, clazz);
    }

    /**
     * 获得项目所部署的项目名称root则会返回空值
     * @return
     */
	public static String getWebappsProjectName() {
		return RestCloudServlet.getWebappsProjectName();
	}
}